package com.pqtest.api.aop;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pqtest.api.annotation.Paging;
import com.pqtest.api.annotation.ResponseFormat;
import lombok.extern.java.Log;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Aspect
@Component
@Log
public class CommonAspect {
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Pointcut("within(com.pqtest.api.controller.*)")
    public void filterDoc() {
    }

    @Around("filterDoc() && @annotation(responseFormat)")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint, ResponseFormat responseFormat) throws Throwable {
        Object object = proceedingJoinPoint.proceed();
        JSONObject jsonObject = new JSONObject();
        switch (responseFormat.type()) {
            case "withTotal":
                PageInfo<?> p = new PageInfo<>((List<?>) object);
                jsonObject.put("code", "ok");
                jsonObject.put("total", p.getTotal());
                jsonObject.put("data", object);
                object = jsonObject;
                break;
            case "withoutTotal":
                jsonObject.put("code", "ok");
                jsonObject.put("data", object);
                object = jsonObject;
                break;
            case "withSingle":
                if (object == null) {
                    jsonObject.put("code", "fail");
                } else {
                    jsonObject.put("code", "ok");
                    jsonObject.put("data", object);
                }
                object = jsonObject;
                break;
            case "withMsg" :
                jsonObject.put("msg",object);
                jsonObject.put("code","ok");
                object = jsonObject;
            break;
        }
        return object;
    }

    @Before("filterDoc() && @annotation(pagination)")
    public void doPagination(Paging pagination) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        int start, limit;
        // int limit
        String reqStart = request.getParameter("skip");
        String reqLimit = request.getParameter("limit");
        if (reqStart == null || reqStart.equals("")) {
            start = pagination.start();
        } else {
            start = Integer.parseInt(reqStart);
        }
        if (reqLimit == null || reqLimit.equals("")) {
            limit = pagination.limit();
        } else {
            limit = Integer.parseInt(reqLimit);
        }
        PageHelper.offsetPage(start, limit);
    }
}
